/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.klarna.raas;

import java.util.Properties;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.Main;
import org.apache.log4j.Logger;

import com.klarna.raas.datamodel.impl.RequestImpl;
import com.klarna.raas.util.RequestHeadersBuilder;

/**
 * A simple example router from a file system to an ActiveMQ queue and then to a file system
 *
 * @version 
 */
public class MyRouteBuilder extends RouteBuilder {
	static final Logger logger = Logger.getLogger(MyRouteBuilder.class);
	/**
	 * Allow this route to be run as an application
	 */
	public static void main(String[] args) throws Exception {
		new Main().run(args);
	}

	public void configure() {
		// route for parsing input file
		from("file:C:\\ws\\raasint\\integraas\\src\\data?fileName=supplierinputdataNoHeader2Entries.csv&noop=true&idempotent=true")
		.split()
		.tokenize("\\n")
		.process(new Processor() {
			public void process(Exchange exchange) throws Exception {
				RequestImpl request = new RequestImpl(exchange.getIn().getBody(String.class));
				RequestHeadersBuilder.buildReqHeaders(exchange, request);
			}
		})
		.to("activemq:req");

		// route to send requests to the BRMS
		from("activemq:req")
		.inOut("activemq:responseQ")
		.process(new Processor() {
			public void process(Exchange exchange) throws Exception {
				logger.info("##### REQUEST START #####");
				logger.info("got the response.");
				
				
				logger.info(ResponseHeaderConstants.RESULT + "=" 
						+ exchange.getIn().getHeader(ResponseHeaderConstants.RESULT));
				logger.info(ResponseHeaderConstants.TIMESTAMP + "=" 
						+ exchange.getIn().getHeader(ResponseHeaderConstants.TIMESTAMP));
				logger.info(ResponseHeaderConstants.VARIABLEVECTOR + "=" 
						+ exchange.getIn().getHeader(ResponseHeaderConstants.VARIABLEVECTOR));

				logger.info("## IN Headers");
				logger.info(exchange.getIn().getHeaders().toString());
				logger.info("## OUT Headers");
				logger.info(exchange.getOut().getHeaders().toString());
				
				logger.info("##### REQUEST END #####");
			}
		});
		
		// route for BRMS replies
		from("activemq:responseQ")
		.process(new Processor() {
			public void process(Exchange exchange) throws Exception {
				logger.info("##### RESPONSE START #####");

				Properties props = new Properties();
				props.setProperty("key1", "value1");
				props.setProperty("key2", "value2");		
				props.setProperty("key3", "value3");

				exchange.getOut().setHeader(ResponseHeaderConstants.RESULT, "YES");
				exchange.getOut().setHeader(ResponseHeaderConstants.TIMESTAMP, System.currentTimeMillis());
				exchange.getOut().setHeader(ResponseHeaderConstants.VARIABLEVECTOR, props.toString());
				
				logger.info("## IN Headers");
				logger.info(exchange.getIn().getHeaders().toString());
				logger.info("## OUT Headers");
				logger.info(exchange.getOut().getHeaders().toString());

				logger.info("##### END OF RESPONSE #####");
			}
		});
	}
}